<template>
  <div class="el-card cardBorder">
    <div class="operate">
      <div class="operate">
        <el-button type="primary" @click="addCourseMapping">添加</el-button>
        <el-select v-model="searchParams.userId" placeholder="请选择学生" style="margin: 0 5px">
          <el-option v-for="(item, index) in students" :key="index" :label="item.nickName"
                     :value="item.userId"></el-option>
        </el-select>
        <el-select v-model="searchParams.courseId" placeholder="请选择课程" style="margin: 0 5px">
          <el-option v-for="(item, index) in courses" :key="index" :label="item.courseName"
                     :value="item.courseId"></el-option>
        </el-select>
        <el-button type="primary" @click="getAllCourseMapping">搜索</el-button>
      </div>
    </div>
    <el-table
        :data="courseMappings"
        border
        style="width: 100%;height: 100%;margin-bottom: 10px">
      <el-table-column prop="id" label="id"></el-table-column>
      <el-table-column prop="user.nickName" label="学生名"></el-table-column>
      <el-table-column prop="course.courseName" label="课程名"></el-table-column>
      <el-table-column prop="createTime" label="选课时间"></el-table-column>
      <el-table-column  label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"  v-if="scope.row.userType!=='0'">退选</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="searchParams.pageNum"
        :page-size="searchParams.pageSize"
        :page-sizes="[5, 8, 10, 20]"
        layout="total, prev, pager, next"
        :total="total">
    </el-pagination>
  <course-mapping-dialog :visible.sync="addDialog" :courses="courses" :users="students" @addResult="getAllCourseMapping"></course-mapping-dialog>
  </div>
</template>

<script>
import {deleteCourse, deleteCourseMapping, getAllCourse, getAllCourseMapping} from "@/api/course";
import {getAllUser} from "@/api/user";
import CourseMappingDialog from "@/components/dialog/CourseMappingDialog";

export default {
  name: "courseResult",
  components: {CourseMappingDialog},
  data(){
    return{
      searchParams:{
        pageNum: 1,
        pageSize: 5,
        courseId:'',
        userId:''
      },
      courseMappings:[],
      students:[],
      courses:[],
      total:10,
      addDialog:false,
      updateCourse:{},
      updateDialog:false
    }
  },
  methods:{
    getAllCourseMapping() {
      getAllCourseMapping(this.searchParams).then(res=>{
        this.courseMappings=res.data.records;
        this.total=res.data.total;
      })
    },
    handleSizeChange(val){
      this.getAllCourseMapping();
    },
    handleCurrentChange(val){
      this.getAllCourseMapping();
    },
    handleDelete(index,row){
      deleteCourseMapping(row.id).then(res=>{
        if (res.code===200){
          this.$message.success("成功退选")
          this.getAllCourseMapping();
        }
      })
    },
    addCourseMapping(){
      this.addDialog=true;
    }
  },
  mounted() {
    this.getAllCourseMapping();
    getAllUser().then(res=>{
      if (res.data!=null){
        for (let i = 0; i < res.data.length; i++) {
          if (res.data[i].userType==='2'){
            this.students.push({'userId':res.data[i].id,'nickName':res.data[i].nickName})
          }
        }
      }
    });
    getAllCourse().then(res=>{
      if (res.data!=null){
        for (let i = 0; i < res.data.length; i++) {
            this.courses.push({'courseId':res.data[i].id,'courseName':res.data[i].courseName})
        }
      }
    })
  }
}
</script>

<style lang="less" scoped>
.cardBorder{
  width: 96%;
  padding: 20px;
.operate{
  margin: 16px;
}
}
</style>
